//
//  AddStudentViewController.m
//  UI7_通讯录归档反归档练习
//
//  Created by Hanlingyu on 16/8/6.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "AddStudentViewController.h"
#import "Student.h"


@interface AddStudentViewController ()

<
    UINavigationControllerDelegate,
    UIImagePickerControllerDelegate
>

@property (weak, nonatomic) IBOutlet UIButton *headIamgeButton;


@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITextField *genderTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (nonatomic, strong) UIImagePickerController *imagePickerViewController;

@end

@implementation AddStudentViewController
- (IBAction)addSutdent:(UIButton *)sender {
    
    Student *student = [[Student alloc] initWithName:_nameTextField.text age:@(_ageTextField.text.doubleValue)  gender:_genderTextField.text phoneNumber:_phoneNumberTextField.text];
    [self.delegate addStudentToArray:student];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];

}
- (IBAction)changeHeaderImage:(UIButton *)sender {

    self.imagePickerViewController = [[UIImagePickerController alloc] init];
    
    _imagePickerViewController.allowsEditing = YES;
    _imagePickerViewController.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
    _imagePickerViewController.delegate = self;
    [self presentViewController:_imagePickerViewController animated:YES completion:^{
        
        
    }];
    
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
    UIImage *editedImage = info[UIImagePickerControllerEditedImage];
    UIImage *savedImage = editedImage ?: originalImage;
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil);
    }
    
    __weak AddStudentViewController *weakSelf = self;
    [picker dismissViewControllerAnimated:YES completion:^{
        [weakSelf.headIamgeButton setBackgroundImage:savedImage forState:UIControlStateNormal];
        weakSelf.headIamgeButton.backgroundColor = [UIColor clearColor];
        
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
